package com.ev.asl.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ev.asl.constant.AslConstant;
import com.ev.asl.service.*;
import com.ev.asl.vo.CheckResultDetailVO;
import com.ev.asl.vo.CheckResultVO;
import com.ev.asl.vo.CheckResultVO2;
import com.ev.asl.vo.CheckResultVO3;
import com.ev.framework.config.Constant;
import com.ev.framework.utils.BeanUtils;
import com.ev.framework.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 姚家乐
 * @since 2021/6/30 8:51
 */
@Service
public class CheckStatisticsServiceImpl implements CheckStatisticsService {

    @Autowired
    private CheckResultService                  checkResultService;

    @Autowired
    private MoldingDailyService                 moldingDailyService;

    @Autowired
    private AssemblyDailyService                assemblyDailyService;

    @Override
    public Map<String,Object> query(Map<String, Object> param) {
//        param.put("type",1);
        List<CheckResultVO> checkResultVOList = checkResultService.getDetailInfoList(param);

        //成型日报ID
        Set<Long> dailyIdMd = checkResultVOList.stream()
            .filter(o -> (AslConstant.ONE.equals(o.getIsChooseShow())
                          && AslConstant.YES.equals(o.getType())))
            .map(CheckResultVO::getDailyId).collect(Collectors.toSet());

        //组装日报ID
        Set<Long> dailyIdAd = checkResultVOList.stream()
            .filter(o -> (AslConstant.TWO.equals(o.getIsChooseShow())
                          && AslConstant.YES.equals(o.getType())))
            .map(CheckResultVO::getDailyId).collect(Collectors.toSet());

        Map<String, Object> param1 = new HashMap<>();
        //查询成型日报
        param1.put("ids", dailyIdMd);
        List<Map<String, Object>> mdList = moldingDailyService.listForMap(param1);
        param1.remove("ids");

        //查询组装日报
        param1.put("ids", dailyIdAd);
        List<Map<String, Object>> adList = assemblyDailyService.listForMap(param1);

        String mGroup = null;
        for (Map<String, Object> md : mdList) {
            Long id = Long.parseLong(String.valueOf(md.get("id")));

            if (String.valueOf(md.get("mGroup")).equals("0")){
                mGroup = "A";
            }
            if (String.valueOf(md.get("mGroup")).equals("0")){
                mGroup = "B";
            }
            if (String.valueOf(md.get("mGroup")).equals("0")){
                mGroup = "C";
            }
            for (CheckResultVO resultVO : checkResultVOList) {
                if (id.equals(resultVO.getDailyId())
                    && AslConstant.ONE.equals(resultVO.getIsChooseShow())
                    && AslConstant.YES.equals(resultVO.getType())) {
                    resultVO.setMGroup(mGroup);
                }
            }
        }

        for (Map<String, Object> ad : adList) {
            Long id = Long.parseLong(String.valueOf(ad.get("id")));
            for (CheckResultVO resultVO : checkResultVOList) {
                if (id.equals(resultVO.getDailyId())
                    && AslConstant.TWO.equals(resultVO.getIsChooseShow())
                    && AslConstant.YES.equals(resultVO.getType())) {
                }
            }
        }

        List<Map<String, Object>> results = new ArrayList<>();
        Map<String, Object> result;

        int size = AslConstant.ZERO;

        for (CheckResultVO vo : checkResultVOList) {
            result = new HashMap<>();
            //日期
            result.put("date", vo.getProductionDate());
            //班次
            result.put("mGroup",vo.getMGroup() != null ? vo.getMGroup() : "");
            //批号
            result.put("batchNo", vo.getBatchNo());
            //物料信息
            result.put("product", vo.getProduct());

            //检验类型层面
            List<Map<String, Object>> two = new ArrayList<>();
            List<CheckResultDetailVO> detailVOList = vo.getDetailVOList();
            Set<String> checkTypes = detailVOList.stream()
                    .map(CheckResultDetailVO::getCheckType).collect(Collectors.toSet());

            Map<String, Object> twoDetail;
            for (String checkType : checkTypes) {
                if (ObjectUtils.isEmpty(checkType))continue;
                twoDetail = new HashMap<>();
                List<CheckResultDetailVO> twoDetailVoList = detailVOList.stream()
                        .filter(o -> checkType.equals(o.getCheckType()))
                        .collect(Collectors.toList());
                twoDetail.put("checkType", checkType);
                twoDetail.put("detail", twoDetailVoList);
                two.add(twoDetail);

                size = size + twoDetailVoList.size();
            }
            result.put("two", two);
            results.add(result);
        }

        Map<String,Object> d = new HashMap<>();
        d.put("data",results);
        d.put("size",size);
        return d;
    }

    @Override
    public Map<String,Object> query2(Map<String, Object> param) {
//        param.put("type",1);
        List<CheckResultVO> checkResultVOList = checkResultService.getDetailInfoList2(param);
        List<CheckResultVO2> results = new ArrayList<>();
        int size = checkResultService.getCountInfoList(param);
        checkResultVOList.forEach(result->{
            CheckResultVO2 vo2 = new CheckResultVO2();
            BeanUtils.copyProperties(result,vo2);
            vo2.setProductionDate(result.getCreateTime());
            List<CheckResultDetailVO> houdu = result.getDetailVOList().stream().filter(detail -> !ObjectUtils.isEmpty(detail.getCheckValue()) && Constant.HOUDU.equals(detail.getCheckItem())).collect(Collectors.toList());
            List<CheckResultDetailVO> keliwu = result.getDetailVOList().stream().filter(detail -> !ObjectUtils.isEmpty(detail.getCheckValue()) && Constant.KELIWU.equals(detail.getCheckItem())).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(houdu)){
                vo2.setCheckValueHD(StringUtils.join(JSON.parseArray(houdu.get(0).getCheckValue()),","));
            }
            if (!ObjectUtils.isEmpty(keliwu)){
                keliwu.forEach(keli ->{
                    if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.3")){
                        vo2.setCheckValueKLW1(StringUtils.join(JSON.parseArray(keli.getCheckValue()),","));
                    }
                    if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.2")){
                        vo2.setCheckValueKLW2(StringUtils.join(JSON.parseArray(keli.getCheckValue()),","));
                    }
                    if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.1")){
                        vo2.setCheckValueKLW3(StringUtils.join(JSON.parseArray(keli.getCheckValue()),","));
                    }
                });
            }
            results.add(vo2);
        });


        Map<String,Object> d = new HashMap<>();
        d.put("data",results);
        d.put("size",size);
        return d;
    }
    @Override
    public Map<String,Object> query3(Map<String, Object> param) {
        Map<String,Object> results = new HashMap<>();
        ArrayList<Map<String, Object>> resultList = new ArrayList<>();

//        param.put("type",1);
        int size = checkResultService.countForMap(param);
        if (size <= 0){
            return results;
        }
        List<CheckResultVO> checkResultVOList = checkResultService.getDetailInfoList2(param);
        checkResultVOList.forEach(check->{
            Map<String,Object> result = new HashMap<>();
            Date productionDate = check.getCreateTime();
            result.put("productionDate",DateUtils.format(productionDate));
            result.put("id",check.getId());
//            result.put("productionDate",check.getBatchNo());
            result.put("batchNo",check.getBatchNo());
            result.put("instructionQty",check.getInstructionQty());
            result.put("qualityInspectorName",check.getQualityInspectorName());
            result.put("qualityBearerName",check.getQualityBearerName());
            if (!ObjectUtils.isEmpty(check.getGroup())){
                if (check.getGroup().equals(0)){
                    result.put("group","A");
                }
                if (check.getGroup().equals(1)){
                    result.put("group","B");
                }
                if (check.getGroup().equals(2)){
                    result.put("group","C");
                }
            }
            result.put("product",check.getProduct());
            List<CheckResultDetailVO> houdu = check.getDetailVOList().stream().filter(detail -> !ObjectUtils.isEmpty(detail.getCheckValue()) && Constant.HOUDU.equals(detail.getCheckItem())).collect(Collectors.toList());
            List<CheckResultDetailVO> keliwu = check.getDetailVOList().stream().filter(detail -> !ObjectUtils.isEmpty(detail.getCheckValue()) && Constant.KELIWU.equals(detail.getCheckItem())).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(houdu)){
                if(!ObjectUtils.isEmpty(houdu.get(0).getCheckValue())){
                    JSONArray array = JSON.parseArray(houdu.get(0).getCheckValue());
                    //厚度1~13
                    for (int i = 0; i < array.size(); i++) {
                        result.put("houdu" + i,array.get(i));
                    }
                }
            }
            if (!ObjectUtils.isEmpty(keliwu)){
                keliwu.forEach(keli ->{
                    if(!ObjectUtils.isEmpty(keli.getCheckValue())){
                        JSONArray array = JSON.parseArray(keli.getCheckValue());
                        if(array.size() > 0){
                            if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.3")){
                                result.put("keliwu1",array.get(0));
//                            result.put("keliwu2",array.get(1));
//                            result.put("keliwu3",array.get(2));
                            }
                            if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.2")){
                                result.put("keliwu4",array.get(0));
//                            result.put("keliwu5",array.get(1));
//                            result.put("keliwu6",array.get(2));
                            }
                            if (!ObjectUtils.isEmpty(keli) && keli.getSize().contains("0.1")){

                                result.put("keliwu7",array.get(0));
//                            result.put("keliwu8",array.get(1));
//                            result.put("keliwu9",array.get(2));
                            }
                        }

                    }
                });
            }
            resultList.add(result);
        });
        results.put("resultList",resultList);
        return results;
    }

    @Override
    public Integer getCountInfoList(Map<String, Object> param) {
//        param.put("type",1);
        return checkResultService.getCountInfoList(param);
    }
}
